package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.secsys.dto.StoStockDTO;
import com.secsys.dto.StoStockdailyDTO;
import com.secsys.model.TbStoStock;
import com.secsys.model.TbStoStockdaily;
import com.secsys.service.StoStockService;
import com.secsys.service.StoStockdailyService;
import com.secsys.util.DateFormatUtils;

public class StoStockdailyServiceImpl extends
		ServiceImpl<TbStoStockdaily, Integer> implements StoStockdailyService {
	private StoStockService stoStockService;
	
	@Override
	public List<StoStockdailyDTO> queryStoStockdailyByStockCode(String stockCode,int offset,int count) {
		List<TbStoStock> list=stoStockService.findByProperty("from TbStoStock where stockCode=?", stockCode);
		if (null==list || list.size()==0) {
			return null;
		}
		TbStoStock tbStoStock=list.get(0);
		return queryStoStockdailyByTbStoStock(tbStoStock,offset,count);
	}

	@Override
	public List<StoStockdailyDTO> queryStoStockdailyByTbStoStock(TbStoStock tbStoStock,int offset,int count) {
		String hql="from TbStoStockdaily as tss where tss.tbStoStock='"+tbStoStock.getId()+"' order by tss.operateDate desc";
		
		List<TbStoStockdaily> list=this.findByHql(hql, offset, count);
		if (null==list || list.size()==0) {
			return null;
		}
		List<StoStockdailyDTO> stoStockdailyDTOList=foreachGetDtoFromPojo(list);	
		return stoStockdailyDTOList;
	}

	
	
	@Override
	public int countByStockCode(String stockCode) {
		List<TbStoStock> list=stoStockService.findByProperty("from TbStoStock where stockCode=?", stockCode);
		if (null==list || list.size()==0) {
			return 0;
		}
		TbStoStock tbStoStock=list.get(0);
		return countByTbStoStock(tbStoStock);
	}

	@Override
	public int countByTbStoStock(TbStoStock tbStoStock) { 
		String hql="select count(*) from TbStoStockdaily as tss where tss.tbStoStock='"+tbStoStock.getId()+"'"; 
		return stoStockService.count(hql);
	}

	@Override
	public List<StoStockdailyDTO> queryStoStockdailyByDate(Date operateDate,int sortBy,int offset,int count) { 
		String hql;
		String date=DateFormatUtils.convertDateToString(operateDate);
		System.out.println("queryStoStockdailyByDate--"+date+"--");  
		if (sortBy==0) {
			hql="from TbStoStockdaily as tss where tss.operateDate<='"+date+"' order by tss.operateDate desc";
		}else {
			hql="from TbStoStockdaily as tss where tss.operateDate<='"+date+"' order by tss.operateDate asc"; 
		}
		return foreachGetDtoFromPojo(this.dao.findBySql(hql, offset, count)); 
	}
	
	public int countSearchWithEndPrice(Date startDate,Date endDate,double rate,int updown){
		String hql=null;
		if (updown==1) {
			hql="from TbStoStockdaily as tss2,TbStoStockdaily as tss1 where " +
			" tss1.tbStoStock=tss2.tbStoStock and  tss1.operateDate=? and tss2.operateDate=? and " +
			" tss2.endPrice > tss1.endPrice and tss2.endPrice/tss1.endPrice >?";
		}else {
			hql="from TbStoStockdaily as tsf2,TbStoStockdaily as tsf1 where " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.operateDate=? and tsf2.operateDate=? and " +
			" tsf2.endPrice < tsf1.endPrice and tsf2.endPrice/tsf1.endPrice >?";
		}
		rate=rate/100+1;
		
		List<Object> list=this.dao.findObject(hql, new Object[]{startDate,endDate,rate});
		List<StoStockdailyDTO> stoStockdailyDTOList=new ArrayList<StoStockdailyDTO>();
		for (Object object : list) {
			StoStockdailyDTO stoStockdailyDTO=getDtoFromPojo((TbStoStockdaily)((Object[])object)[0]); 
			stoStockdailyDTOList.add(stoStockdailyDTO);
		}
		return list.size();
	}
	public HashMap<String, List> querySearchWithEndPrice(Date startDate,Date endDate,double rate,int updown,int offset,int count){
		String hql=null;
		if (updown==1) {
			hql="from TbStoStockdaily as tss2 inner join tss2.tbStoStock,TbStoStockdaily as tss1 where " +
			" tss1.tbStoStock=tss2.tbStoStock and  tss1.operateDate=? and tss2.operateDate=? and " +
			" tss2.endPrice > tss1.endPrice and tss2.endPrice/tss1.endPrice >? order by tss2.endPrice/tss1.endPrice desc";
		}else {
			hql="from TbStoStockdaily as tsf2 inner join tss2.tbStoStock,TbStoStockdaily as tsf1 where " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.operateDate=? and tsf2.operateDate=? and " +
			" tsf2.endPrice < tsf1.endPrice and tsf2.endPrice/tsf1.endPrice >?";
		}
		rate=rate/100+1;
		List<Object> list=this.dao.findObject(hql, new Object[]{startDate,endDate,rate},offset,count);
		List<StoStockdailyDTO> stoStockdailyDTOList=new ArrayList<StoStockdailyDTO>();
		List<StoStockdailyDTO> oldYearStoStockdailyDTOList=new ArrayList<StoStockdailyDTO>();
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		for (Object object : list) {
			StoStockdailyDTO stoStockdailyDTO=getDtoFromPojo((TbStoStockdaily)((Object[])object)[0]); 
			stoStockdailyDTOList.add(stoStockdailyDTO);
			StoStockDTO stockDTO=stoStockService.getDtoFromPojo((TbStoStock)((Object[])object)[1]);
			stockDTOList.add(stockDTO);
			StoStockdailyDTO oldYearStoStockdailyDTO=getDtoFromPojo((TbStoStockdaily)((Object[])object)[2]); 
			oldYearStoStockdailyDTOList.add(oldYearStoStockdailyDTO);
		}
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stoStockdailyDTOList", stoStockdailyDTOList);
		map.put("stockDTOList", stockDTOList); 
		map.put("oldYearStoStockdailyDTOList", oldYearStoStockdailyDTOList); 
		return map; 
	}
	
	
	public HashMap<String, List> queryTopPrice(Date date,int sortBy,int offset,int count){
		String hql="";
		if (sortBy==1) {
			hql="  SELECT id as ID,t.tbStoStock as STOCKID,t.operateDate as OPERATEDATE,t.startPrice as STARTPRICE,t.endPrice as ENDPRICE" +
					" FROM TbStoStockdaily t where t.operateDate=? " +
					" group by t.tbStoStock order by t.operateDate asc limit= '"+count+"' offset= '" +" offset '"; 
		}else {
			hql="  SELECT id as ID,t.tbStoStock as STOCKID,t.operateDate as OPERATEDATE,t.startPrice as STARTPRICE,t.endPrice as ENDPRICE" +
			" FROM TbStoStockdaily t where t.operateDate=? " +
			" group by t.tbStoStock order by t.operateDate desc limit= '"+count+"' offset= '" +" offset '"; 
		}
		List<Object> list=null;
		try {
			list=this.dao.findObject(hql, new Object[]{date},offset,count);
			System.out.println("size:"+list.size()); 
		} catch (Exception e) {
			System.out.println("hql-----"+hql);
			e.printStackTrace();
		}
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		List<Integer> stockIDList=new ArrayList<Integer>(); 
		List<Date> operateDateList=new ArrayList<Date>();
		List<Double> startPriceList=new ArrayList<Double>();
		List<Double> endPriceList=new ArrayList<Double>();
		for (Object object : list) { 
			Object[] o=(Object[])object; 
			Integer i=(Integer)o[0]; 
			stockIDList.add(i);
			stockDTOList.add(new StoStockDTO((TbStoStock)o[1]));
			operateDateList.add((Date)o[2]);
			startPriceList.add((Double)o[3]);
			endPriceList.add((Double)o[4]);
		}
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stockDTOList", stockDTOList);
		map.put("stockIDList", stockIDList); 
		map.put("operateDateList", operateDateList); 
		map.put("startPriceList", startPriceList); 
		map.put("endPriceList", endPriceList); 
		return map;
	}
	
	@Override
	public Date findLastDate() {
		String sqlString="select Max(operateDate) from TbStoStockdaily";
		List<Object> oList=this.dao.findObject(sqlString);
		Date date=null;
		if (oList!=null && oList.size()>0) {
			date=(Date)oList.get(0); 
		}
		return date;
	}
	
	@Override
	public List<StoStockdailyDTO> queryStoStockdailyByTbStoStockCodeAndDate(
			int stockCode, Date operateDate) {
		TbStoStock tbStoStock=stoStockService.findByProperty("from TbStoStock where stockCode=?", String.valueOf(stockCode)).get(0);
		return queryStoStockdailyByTbStoStockAndDate(tbStoStock,operateDate);
	}

	@Override
	public List<StoStockdailyDTO> queryStoStockdailyByTbStoStockAndDate(
			TbStoStock tbStoStock, Date operateDate) {
		String hql="from TbStoStockdaily as tss where tss.tbStoStock=? and tss.operateDate=?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{tbStoStock,operateDate})); 
	}

	@Override
	public List<StoStockdailyDTO> queryStoStockdailyByVolume(double startVlume,	double endVolume, int offset, int count) {
		String hql="from TbStoStockdaily as tss where tss.volume between ? and ?";
		return foreachGetDtoFromPojo(this.dao.findBySql(hql, new Object[]{startVlume,endVolume},offset,count));  
	}
	
	@Override
	public List<StoStockdailyDTO> queryStoStockdailyByAmount(
			double startAmount, double endAmount, int offset, int count) {
		String hql="from TbStoStockdaily as tss where tss.amount between ? and ?";
		return foreachGetDtoFromPojo(this.dao.findBySql(hql, new Object[]{startAmount,endAmount},offset,count));  
	}

	/**
	 * 批量地将持久化对象POJO的tbStoStockdaily转换成领域对象DTO的StoStockdailyDTO
	 * @param tbStoStockdailyList
	 * 			持久化对象POJO的tbStoStockdaily的List
	 * @return	List<StoStockdailyDTO>
	 * 			领域对象DTO的StoStockdailyDTO的List
	 */
	private List<StoStockdailyDTO> foreachGetDtoFromPojo(List<TbStoStockdaily> tbStoStockdailyList){
		List<StoStockdailyDTO> stoStockdailyDTOList=new ArrayList<StoStockdailyDTO>();
		for (TbStoStockdaily tbStoStockdaily : tbStoStockdailyList) {
			stoStockdailyDTOList.add(getDtoFromPojo(tbStoStockdaily));
		}
		return stoStockdailyDTOList;
	}
	
	/**
	 * 将持久化对象POJO的tbStoStockdaily转换成领域对象DTO的StoStockdailyDTO
	 * @param tbStoStockdaily
	 * 			持久化对象POJO的tbStoStockdaily
	 * @return	StoStockdailyDTO
	 * 			领域对象DTO的StoStockdailyDTO
	 */
	private StoStockdailyDTO getDtoFromPojo(TbStoStockdaily tbStoStockdaily){
		StoStockdailyDTO stoStockdailyDTO=new StoStockdailyDTO(
				tbStoStockdaily.getId(),tbStoStockdaily.getOperateDate(),
				tbStoStockdaily.getStartPrice(),tbStoStockdaily.getMaxPrice(),
				tbStoStockdaily.getMinPrice(),tbStoStockdaily.getEndPrice(),
				tbStoStockdaily.getAmount(),tbStoStockdaily.getVolume()
		);
		return stoStockdailyDTO;
	}

	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}
}
